<?php slot('sub', $sub); ?>
<?php  use_javascript('mainFunctions.js');?>
<?php  use_javascript('admins_script.js');?>
<?php  use_javascript('http://www.google.com/jsapi');?>
<?php  use_javascript('http://maps.googleapis.com/maps/api/js?key=AIzaSyA4ORL54bd4TqneYqWekkc22xhZ1T3gH6A&sensor=true');?>

<div class="center">
    <div id="center_conteiner" class="clearfix">
        <div class="center_sub_conteiner">

            <div class="center_block">
<div class="header_center_block">
                    <span>Create new agent / firm</span>
                    <div class="back_button"><a href="<?php echo url_for('ausers'); ?>">Back</a></div>
 </div>             
<div class="aedituser">               
<div class="aedituser_left">              
                

                <div class="center_line" id="edit_user_id">
                    <div class="field_label">ID</div>
                    <input type="text" class="field_input" value="<?php echo $user->getId(); ?>" readonly>
                </div>

                <div class="center_line" id="edit_user_title">
                    <div class="field_label">Title</div>
                    <input type="text" value="<?php echo $user->getTitle(); ?>" class="field_input">
                </div>

                <div class="center_line" id="edit_user_first_name">
                    <div class="field_label">First name</div>
                    <input type="text" class="field_input" value="<?php echo $user->getFirstName(); ?>">
                </div>

                <div class="center_line"  id="edit_user_last_name">
                    <div class="field_label">Last name</div>
                    <input type="text" class="field_input" value="<?php echo $user->getLastName(); ?>">
                </div>

                <div class="center_line" id="edit_user_email">
                    <div class="field_label">E-mail</div>
                    <input type="text" class="field_input" value="<?php echo $user->getEmail(); ?>">
                </div>

                <div class="center_line" id="edit_user_password">
                    <div class="field_label">Password</div>
                    <input type="text" class="field_input">
                </div>

                <div class="center_line"  id="edit_user_vpassword">
                    <div class="field_label">Verify Password</div>
                    <input type="text" class="field_input">
                </div>

                <div class="center_line"  id="edit_user_phone1">
                    <div class="field_label">Phone</div>
                    <input type="text" class="field_input" value="<?php echo $user->getPhone1(); ?>">
                </div>

                <div class="center_line" id="edit_user_phone2">
                    <div class="field_label">Phone</div>
                    <input type="text" class="field_input" value="<?php echo $user->getPhone2(); ?>">
                </div>

                <div class="center_line"  id="edit_user_fax">
                    <div class="field_label">Fax</div>
                    <input type="text" class="field_input" value="<?php echo $user->getFax(); ?>">
                </div>

                <div class="center_line" id="edit_user_country">
                    <div class="field_label">Country</div>
                    <select class="field_select">
                        <?php
                        foreach ($country as $c) {
                            if ($user->getCountry() === $c->getId()) {
                                echo '<option selected="selected" value="' . $c->getId() . '">' . $c->getNameEn() . '</option>';
                            } else {
                                echo '<option value="' . $c->getId() . '">' . $c->getNameEn() . '</option>';
                            }
                        }
                        ?>
                    </select>
                </div>

                <div class="center_line" id="edit_user_city">
                    <div class="field_label">City</div>
                    <select class="field_select">
                        <?php
                        foreach ($city as $c) {
                            if ($user->getCity() === $c->getId()) {
                                echo '<option selected="selected" value="' . $c->getId() . '">' . $c->getNameEn() . '</option>';
                            } else {
                                echo '<option value="' . $c->getId() . '">' . $c->getNameEn() . '</option>';
                            }
                        }
                        ?>
                    </select>
                </div>

                <div class="center_line"  id="edit_user_address">
                    <div class="field_label">Address</div>
                    <input type="text" class="field_input" value="<?php echo $user->getAddress(); ?>">
                </div>

                <div class="center_line" id="edit_user_permissions">
                    <div class="field_label">Permissions</div>
                    <select class="field_select">
                        <?php
                        if ($user->getPermissions() == sbValues::PERMISSIONS_STANDART) {
                            echo '<option selected="selected" value="'.sbValues::TYPE_USER_STANDART.'">'.sbValues::TYPE_USER_STANDART.'</option>';
                        } else {
                            echo '<option value="'.sbValues::TYPE_USER_STANDART.'">'.sbValues::TYPE_USER_STANDART.'</option>';
                        }

                        if ($user->getPermissions() == sbValues::PERMISSIONS_AGENT) {
                            echo '<option selected="selected" value="'.sbValues::TYPE_USER_AGENT.'">'.sbValues::TYPE_USER_AGENT.'</option>';
                        } else {
                            echo '<option value="'.sbValues::TYPE_USER_AGENT.'">'.sbValues::TYPE_USER_AGENT.'</option>';
                        }

                        if ($user->getPermissions() == sbValues::PERMISSIONS_FIRM) {
                            echo '<option selected="selected" value="'.sbValues::TYPE_USER_FIRM.'">'.sbValues::TYPE_USER_FIRM.'</option>';
                        } else {
                            echo '<option value="'.sbValues::TYPE_USER_FIRM.'">'.sbValues::TYPE_USER_FIRM.'</option>';
                        }

                        if ($user->getPermissions() == sbValues::PERMISSIONS_ADMIN) {
                            echo '<option selected="selected" value="'.sbValues::TYPE_USER_ADMIN.'">'.sbValues::TYPE_USER_ADMIN.'</option>';
                        } else {
                            echo '<option value="'.sbValues::TYPE_USER_ADMIN.'">'.sbValues::TYPE_USER_ADMIN.'</option>';
                        }
                        ?>
                    </select>
                </div>

                <div class="center_line" id="edit_user_agent_from">
                    <div class="field_label">Agent</div>
                    <select class="field_select">
                        <?php
                     
                        #echo $structure->getMainId()."<hr>";
                         foreach ($firm as $f) {
                           if ($structure->getMainId() == $f->getId()) {
                                echo '<option selected="selected" value="' . $f->getId() . '">' . $f->getTitle() . '</option>';
                            } else {
                                echo '<option value="' . $f->getId() . '">' . $f->getTitle() . '</option>';
                            }
                        }
                        ?>
                    </select>
                </div>

                <div class="center_line" id="edit_user_status">
                    <div class="field_label">Status</div>
                    <select class="field_select">
                        <?php 
                        if ($user->getStatus() == sbValues::STATUS_ACTIVE) {
                            echo '<option selected="selected" value="1">active</option>';
                        } else {
                            echo '<option value="1">active</option>';
                        }
                        
                        if ($user->getStatus() == sbValues::STATUS_DEACTIVE) {
                            echo '<option selected="selected" value="2">deactivated</option>';
                        } else {
                            echo '<option value="2">deactivated</option>';
                        }
                        ?>
                    </select>
                </div>
                
                <div class="center_line" id="add_new_user_commisions">
                    <div class="field_label">Commissions %</div>
                    <input type="text" class="field_input" value="<?php echo $user->getCommissions(); ?>">
                </div>

                <div class="center_line">
                    
                    <div class="field_button_positive" id="edit_user_btn_save">Save</div>
                    <div class="field_button_negative" id="edit_user_btn_cancel"><a href="<?php echo url_for('ausers'); ?>">Cancel</a></div>
                </div>

            </div><!--end left info block-->
            
            <div class="aedituser_right" id="agents_map"></div>
            <input type="hidden" name="lat" id="lat" value="<?php echo $user->getLatitude();?>">
            <input type="hidden" name="lng" id="lng" value="<?php echo $user->getLogitude();?>">
           <script>
$( document ).ready(function() {  

find_agent_address('#edit_',true);      
$('#edit_user_address .field_input').change(function (){
    find_agent_address('#edit_',false)
});
$('#edit_user_city .field_select').change(function (){
    find_agent_address('#edit_',false)
});
$('#edit_user_country .field_select').change(function (){
    find_agent_address('#edit_',false)
});
});
</script>

</div>
        </div><!--end central block-->
            
            
        </div>
    </div>
</div>

<script  type="text/javascript">
    var cites = [];

<?php foreach ($city as $c) { ?>
        var temp = {
            id: '<?php echo $c->getId() ?>',
            nameEn: '<?php echo $c->getNameEn() ?>',
            country_id: '<?php echo $c->getCountryId() ?>',
            region_id: '<?php echo $c->getRegionId() ?>'
        };

        cites.push(temp);
<?php } ?>
    var user_city_id = '<?php echo $user->getCity() ?>';
    var user_country_id = '<?php echo $user->getCountry() ?>';

    $(function() {
        insertCity(user_country_id);

        var countryId = 0;
        $('#edit_user_country select').change(function() {
            countryId = $(this).val();
            insertCity(countryId);
        });
    });

    function insertCity(country_id) {
        $('#edit_user_city select option').remove();
        var parentSelect = $('#edit_user_city select');

        $.each(cites, function(i, v) {
            if (parseInt(country_id) === 0) {
                    (parentSelect).append('<option value="' + v.id + '">' + v.nameEn + '</option>');
            } else {
                if (parseInt(v.country_id) === parseInt(country_id)) {
                    if (parseInt(v.id) === parseInt(user_city_id)) {
                        (parentSelect).append('<option value="' + v.id + '" selected="selected">' + v.nameEn + '</option>');
                    } else {
                        (parentSelect).append('<option value="' + v.id + '">' + v.nameEn + '</option>');
                    }
                }
            }
        });
    }
</script>
